/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fourbox;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hartono
 */
public class ProtocolMessage {
    
    public static final int SERVER_MSG_CONNECTED = 0; // ack tanpa permintaan, menandakan bahwa hidup
    
    public static final int SERVER_ACK_LOGIN = 1;
    public static final int SERVER_ACK_LOGOUT = 2;
    
    public static final int SERVER_ACK_KEEP_ALIVE  =3; // ack dari MSG_KEEP_ALIVE                
    
    public static final int SERVER_MSG_UPDATE  = 4; // msg dari server yang menyuruh client untuk melakukan update
    public static final int SERVER_ACK_MSG_LIST_FILES_CHANGES = 5; // ack kalo server sudah menyesuaikan sinkronisasi dari klien
    
    public static final int SERVER_ACK_MSG_LIST_FILES_REGULAR = 6; // ack kalo server sudah memperhatikan database, ada perubahan atau ga
    
    
    public static final int CLIENT_MSG_LOGIN = 10; // msg dari client kalo ingin login
    public static final int CLIENT_MSG_LOGOUT = 11; // msg dari client kalo ingin logout    
    public static final int CLIENT_MSG_LIST_FILES_CHANGES = 13; // msg dari client kalo ada perubahan
    public static final int CLIENT_MSG_LIST_FILES_REGULAR = 15; 
        // msg dari client , kirim secara regular,list file tersebut bakalan dipakai oleh server
    // untuk mengecek ada perubahan atau ga.
    // kalau ada perubahan yang harus di update, berarti update
    
    public static final int CLIENT_MSG_KEEP_ALIVE = 12; 
    // msg untuk kasih tau bahwa dia hidup    , kalo ga kirim2, timeout
    
    // dipanggil server
    public static void createACKMsgListFilesRegular(List queue, ObjectOutputStream out){
        try {
            // queue yang harus dikirim client ke server
            out.writeInt(2); // len
            out.write(SERVER_ACK_MSG_LIST_FILES_REGULAR);
            int size= queue.size();
            out.write(size);
            for (int i=0; i< size; i++){
                out.writeObject(queue.get(i));
            }            
            out.flush();
        } catch (IOException ex) {
            try {                
                Logger.getLogger(ProtocolMessage.class.getName()).log(Level.SEVERE, null, ex);
                out.writeInt(2); // len
                out.write(SERVER_ACK_MSG_LIST_FILES_REGULAR);
                out.write(0);
                out.flush();
                System.out.println("create ACK EXCEPTION");
            } catch (IOException ex1) {
                Logger.getLogger(ProtocolMessage.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        
        
    }
            
    public static void createListFilesMsgClient(String contextPath, Path path, ObjectOutputStream out){        
         
        try {
            FourFileVisitor visitor =  new FourFileVisitor();
            Files.walkFileTree(path, visitor);
            visitor.list.remove(0);            
//            System.out.println("size "+visitor.size);
            
            out.writeInt(2);//len
            out.write(CLIENT_MSG_LIST_FILES_REGULAR);// msg id
            out.write(visitor.list.size());// visitor size
            
            for (int i=0; i< visitor.list.size();i++){
                Path p = (Path)visitor.list.get(i);
                Metafile m = Metafile.createMetafile(contextPath,p.toFile() );
                out.writeObject(m);
            }
            out.flush();
            
        } catch (IOException ex) {
            Logger.getLogger(ProtocolMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }   
    
    static class FourFileVisitor extends SimpleFileVisitor<Path> {
                
        public List list ;

        public FourFileVisitor() {
            list = Collections.synchronizedList(new ArrayList());
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            list.add(dir);            
            return super.preVisitDirectory(dir, attrs);
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {            
            list.add(file);
            return super.visitFile(file, attrs);
        }
        
        
        
    }
}
